﻿using DataAccess;
using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
    public class MatHangDAO
    {
        public static List<MatHangDTO> LoadDanhSachMatHang()
        {
            List<MatHangDTO> list = new List<MatHangDTO>();
            string sql = "SELECT * FROM MATHANG";
            DataTable kq = DataConnection.ExecQuery(sql);
            if (kq.Rows.Count > 0)
            {
                for (int i = 0; i < kq.Rows.Count; i++)
                {
                    DataRow dr = kq.Rows[i];
                    MatHangDTO dd = new MatHangDTO();
                    dd.MaMatHang = Convert.ToInt64(dr["MaMatHang"]);
                    dd.TenMatHang = dr["TenMatHang"].ToString();
                    list.Add(dd);
                }
            }
            return list;
        }

        public static bool CapNhatSoLuong(int p, int slton)
        {
            string query = "UPDATE DONVITINH SET SoLuongTon='"+slton+"' WHERE MaDonViTinh='"+p+"'";
            try
            {
                if (DataConnection.ExecQuery(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
